<template>
  <div>
    <el-dialog
      v-if="dialogFormVisible"
      :visible="dialogFormVisible"
      :show-close="false"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        :label-width="formLabelWidth"
      >
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="form.code" autocomplete="off" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="form.manager" placeholder="请选择">
            <el-option
              v-for="item in manager"
              :key="item.id"
              :label="item.username"
              :value="item.username"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="form.introduce" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          @click="$emit('update:dialogFormVisible', false)"
        >取 消</el-button>
        <el-button type="primary" @click="dialogChange">确 定</el-button>
      </div>
      <div slot="title" class="dialog-title">
        <p>{{ isEdit ? '编辑部门' : '添加部门' }}</p>
        <span
          class="el-icon-close"
          @click="$emit('update:dialogFormVisible', false)"
        />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addDepartmentsAPI, updateDepartmentsAPI } from '@/api/departments'
export default {
  name: '',
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false
    },
    manager: {
      type: Array,
      default: () => []
    },
    id: {
      type: [Number, String],
      default: ''
    },
    departItem: {
      type: Object,
      default: () => {}
    },
    isEdit: {
      type: Boolean
    }
  },
  data() {
    return {
      form: {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      },
      formLabelWidth: '120px',
      rules: {
        code: [{ required: true, message: '请输入部门编码', trigger: 'blur' }],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请选择部门负责人', trigger: 'blur' }
        ],
        name: [{ required: true, message: '请输入部门名称', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  watch: {
    departItem(newVal) {
      // 如果点击编辑则赋值,添加不赋值
      this.form = newVal
    }
  },
  created() {},
  methods: {
    dialogChange() {
      this.$refs.form.validate(async(valid) => {
        if (!valid) return this.$message.error('请输入内容')
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    async doAdd() {
      this.form.pid = this.id
      await addDepartmentsAPI(this.form)
      this.$emit('update:dialogFormVisible', false)
      this.$emit('success')
    },
    async doEdit() {
      await updateDepartmentsAPI(this.form)
      this.$emit('update:dialogFormVisible', false)
      this.$emit('success')
    }
  }
}
</script>
<style lang="scss" scoped>
.dialog-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
